package client;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class DeviceImpl {

    public DeviceImpl() {}

    public String getFunctions() {
        return getContentFile(new File("F:/TCC/functions.xml"));
    }

    public String getForm(String formId) {
        // TODO escolher o form.xml pelo id
        return getContentFile(new File("F:/TCC/form.xml"));
    }

    public String getMessage(String key) {
        if (key.equals("getFunctions")) {
            return getFunctions();
        }
        if (key.startsWith("getForm")) {
            //o formato deve ser getForm:formName
            final String formID = key.split(":")[1];
            return getForm(formID);
        }
        return "Command not found";
    }

    public void executeFunction(String funcID) {
        //TODO executar a funcao
        if ("FCN1".equals(funcID)) {
            //			shutdown();
        } else if ("FCN2".equals(funcID)) {
            //			hibernate();
        }
    }

    private static String getContentFile(File file) {
        StringBuilder sb = new StringBuilder();
        sb.append("BOF");
        try {
            FileReader in = new FileReader(file);
            BufferedReader reader = new BufferedReader(in);

            while (reader.ready()) {
                sb.append(reader.readLine());
                sb.append("\n");
            }

            reader.close();
            in.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        sb.append("EOF");
        return sb.toString();
    }
}
